﻿

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TunnelClasses;
using DevComponents.AdvTree;
using ZedGraph;
using System.IO;
using System.Threading;
using Tunnel_Cross_Section.menue_frm;
using Tunnel_Cross_Section.相关类;
using DevComponents.DotNetBar;
using System.Runtime.InteropServices;


namespace Tunnel_Cross_Section
{
    public partial class add_in_frm : Office2007Form
    {
        
        DataBase m_db;
        string table;
        tunnelSection section;
        do_section_frm section_frm;
        CAD_Class cad = null;
        double H, V, BH, BV;
        string path;
        bool zed_cursor = false;
        Projects m_prj;
        string RL;
        public int no = 0;
        #region 构造函数
        public add_in_frm(do_section_frm frm,string l)
        {
            InitializeComponent();
            //zed初始化
            initial_zed();
            section_frm = frm;
            m_db = section_frm.m_db;
            table = "qj_" + section_frm.m_prj.id.ToString();
            m_prj = section_frm.m_prj;
            no = frm.no;
            this.cad = frm.cad;
        }
        public add_in_frm(do_section_frm frm, unsolved un,string l)
        {
            InitializeComponent();

            //zed初始化
            initial_zed();
            section_frm = frm;
            if (section_frm.LR == "L")
            {
                ComBo.SelectedIndex = 0;
            }
            else if (section_frm.LR == "R")
            {
                ComBo.SelectedIndex = 1;
            }
            m_db = section_frm.m_db;
            path = un.path;
            table = "qj_" + section_frm.m_prj.id.ToString();
            m_prj = section_frm.m_prj;
            no = frm.no;
            this.cad = frm.cad;
            read_raw_file(path);
           
            

        }
        void initial_zed()
        {
            GraphPane myPane = zed.GraphPane;
            zed.GraphPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 45.0F);
            myPane.XAxis.MajorGrid.IsVisible = true;
            myPane.YAxis.MajorGrid.IsVisible = true;
            myPane.XAxis.Title.Text = "H坐标";
            myPane.YAxis.Title.Text = "V坐标";
            myPane.Legend.IsVisible = false;
        }

        #endregion



        #region Page1
        //点击导入原始数据
        private void btn_import_Click(object sender, EventArgs e)
        {
            if (ComBo.SelectedIndex < 0)
            {
                MessageBox.Show("没有选择上下行!");
                return;
            }
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "RAW 文件(*.raw)|*.raw";
            if (op.ShowDialog() == DialogResult.OK)
            {
                path = op.FileName;
                read_raw_file(path);
            }
            
        }
        //读raw文件
        private void read_raw_file(string path)
        {
            //要添加的 的 的 的
            FileInfo info = new FileInfo(path);
            string ParentDirectory = info.Directory.Name;
            Calibrate calib = new Calibrate(m_db, ParentDirectory, info.CreationTime);
            if (calib.IsDevicExist() == false)
            {
                MessageBox.Show("不存在数据库校准记录中,父目录需为仪器编号!");
            }
            section = new tunnelSection(path, calib.Cvalue, no > 1 ? false : true);
      
            //修改该点的名字
            if (ComBo.SelectedIndex == 0)
            {
                
                if (section_frm.LR.Length > 0)
                {
                    RL = section_frm.LR;
                }
                else
                {
                    RL = "L";
                }
                section.SectNo = RL+ section.SectNo;
            }
            else
            {
                if (section_frm.LR.Length > 0)
                {
                    RL = section_frm.LR;
                }
                else
                {
                    RL = "R";
                }

                section.SectNo = RL + section.SectNo;
            }
            string sql;
            if (no == 1)
            {
                sql = "Delete from [" + table + "]  where [断面号]='" + section.SectNo + "'";
                m_db.ExecuteSQL(sql);
            }
            sql = "select [BH],[BV],[H],[V],[沉降点号] from [" + table + "] where [断面号]='" 
                + section.SectNo + "'";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];

            if (dt.Rows.Count > 0 && dt.Rows[0][0].ToString() != "-1" && no > 1)
            {

                //如果坐标系已经存在,那么就平移坐标,并且赋值.
                // exsit = true;
                BH = Convert.ToDouble(dt.Rows[0][0]);
                BV = Convert.ToDouble(dt.Rows[0][1]);
                H = Convert.ToDouble(dt.Rows[0][2]);
                V = Convert.ToDouble(dt.Rows[0][3]);
                section.ButtomCenter = new TSPCoor(V, H);
                double dv = BV + section.SectPnts.InstrHeight;
                //平移坐标系
                section.SectPnts.Adjudt_Unified(dv);
                section.ButtomCenter = new TSPCoor(V, H);
                section.Four_circle_center = new TSPCoor(V, H);
                if (no > 1)
                {
                    string table3 = "qj_" + m_prj.id.ToString() + "_1_data";
                    sql = "Select * from [" + table3 + "] where [断面号]='" + section.SectNo 
                        + "' order by [角度]";
                    System.Data.DataTable ftable = m_db.GetDataSet(sql).Tables[0];
                    List<SectionPoint> fpoints = new List<SectionPoint>();
                    for (int i = 0; i < 6; i++)
                    {
                        SectionPoint point = new SectionPoint();
                        point.Coor.Coor_H = Convert.ToDouble(ftable.Rows[i]["预留1"]) + H;
                        point.Coor.Coor_V = Convert.ToDouble(ftable.Rows[i]["预留2"]) + V;
                        point.Hv = point.AzimFrom(section.ButtomCenter);
                        fpoints.Add(point);
                    }
                    //把数据库中读入的缝点存入section中去
                    if (section.F_Section.Count == 0)
                    {
                        section.F_Section = fpoints;
                        section.F_section_config();
                    }
                }

            }
            section.SectPnts.SortByHv();
            load_tree_nodes();
            Draw_ZedGraph();
						            

        }

        private void load_tree_nodes()
        {
            advTree.Nodes.Clear();
            if (section.SectPnts.Points.Count == 0)
            {
                MessageBox.Show("点数为零.....");
                return;
            }
            for (int i = 0; i < section.SectPnts.Points.Count; i++)
            {
                Node n = new Node();
                n.Text = section.SectPnts.Points[i].Name;
                double az = SvAngle.RadToDms(section.SectPnts.Points[i].Hv);
                az = Math.Round(az, 4);
                n.Cells.Add(new Cell(az.ToString()));
                double v = section.SectPnts.Points[i].Coor.Coor_V;
                double h = section.SectPnts.Points[i].Coor.Coor_H;
                v = Math.Round(v, 4);
                h = Math.Round(h, 4);
               
                n.Cells.Add(new Cell(v.ToString()));
                n.Cells.Add(new Cell(h.ToString()));
                advTree.Nodes.Add(n);
            }
            advTree.Refresh();
          



        }
        //画图
        private void Draw_ZedGraph()
        {
            GraphPane myPane = zed.GraphPane;
            myPane.CurveList.Clear();
            myPane.Title.Text = "第" + section.SectNo + "环";
            PointPairList list = new PointPairList();
          
            for (int i = 0; i < section.SectPnts.Points.Count; i++)
            {
                double v = section.SectPnts.Points[i].Coor.Coor_V;
                double h = section.SectPnts.Points[i].Coor.Coor_H;
                list.Add(h, v);
            }
            LineItem myCurve2 = myPane.AddCurve("观测数据", list, Color.Cyan,
                              SymbolType.Diamond);
            myCurve2.Symbol.Fill = new Fill(Color.DarkBlue);
            //myCurve2.Line.IsVisible = false;
            zed.AxisChange();
            zed.Refresh();
           
            
            
 
        }


        //剔粗差
        private void btn_eliminate_Click(object sender, EventArgs e)
        {

            section.Kill_Main_Err();
            load_tree_nodes();
            Draw_ZedGraph();
            Label.Text = "粗差剔除完成.";
        }


        List<TunnelSectionPoints> Section_Pts;//用来装载拟合完成后的数据
        //对环片进行拟合
        private void btn_simulate_Click(object sender, EventArgs e)
        {
            simulate();
           
   
        }
        void simulate()
        {
            if (section == null)
            {
                return;
            }
            Section_Pts = section.eleminate_Err();
            load_tree_nodes();
            Draw_ZedGraph();
            if (Show_result() == false)
            {
               
                Label.Text = "拟合不成功!";
                return;
            }
           ;

            Label.Text = "拟合完成.";
            //然后计算最终结果
            Thread th = new Thread(new ThreadStart(show_final_result));
            th.Start();
        }

        bool Show_result()
        {
            tree_ruslt.Nodes.Clear();
            Button.Text = "第 " + section.SectNo + "环";
            if (Section_Pts.Count == 0 )
            {
                MessageBox.Show("我擦!\r\n没有数据!");
                return false;
            }


            for (int i = 0; i < Section_Pts.Count; i++)
            {
                double temp;
                Node n = new Node();
                temp = i + 1;
                n.Text = "第" + temp.ToString() + "弧";
                if (i == 2 && Section_Pts[2].Points.Count == 0)
                {
                    MessageBox.Show("顶你个肺!\r\n顶上没有数据!");
                    return false;

                }
                else
                {
                    if (Section_Pts[i].Points.Count < 3)
                    {
                        MessageBox.Show((i + 1).ToString() + "环上被剔的一干二净了.");
                        return false;

                    }
                    temp = SvAngle.RadToDms(Section_Pts[i].Points[0].Hv);
                    temp = Math.Round(temp, 4);
                    n.Cells.Add(new Cell(temp.ToString()));
                    temp = SvAngle.RadToDms(Section_Pts[i].Points[Section_Pts[i].Points.Count - 1].Hv);
                    temp = Math.Round(temp, 4);
                    n.Cells.Add(new Cell(temp.ToString()));
                    temp = Math.Round(Section_Pts[i].center.Coor_H, 4);
                    n.Cells.Add(new Cell(temp.ToString()));
                    temp = Math.Round(Section_Pts[i].center.Coor_V, 4);
                    n.Cells.Add(new Cell(temp.ToString()));
                    temp = Math.Round(Section_Pts[i].Radius, 4);
                    n.Cells.Add(new Cell(temp.ToString()));
                    temp = Math.Round(Section_Pts[i].StdErr * 1000, 1);
                    n.Cells.Add(new Cell(temp.ToString()));
                }

                tree_ruslt.Nodes.Add(n);
            }
            tree_ruslt.Refresh();
            return true;
        }
        //显示最终计算结果的函数
        private void show_final_result()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            tree_re.Nodes.Clear();

            tabControlPanel4.UseWaitCursor = true;
            if (Section_Pts[2].Points.Count == 0)
            {
                MessageBox.Show("顶上数据点不够,无法计算结果!");
                return;
            }
            cad.Section_Pts = Section_Pts;
            if (H == 0 || section_frm.no == 1)
            {
                cad.center = section.Four_circle_center;
            }
            else
            {
                if (Math.Abs(V - section.Four_circle_center.Coor_V) < 0.1)
                {
                    cad.center = new TSPCoor(V, H);
                }
                else
                {
                    MessageBox.Show("拟合有误,无法计算结果!");
                }
            }
            cad.config(section);
           
            if (section.F_Section.Count > 0)
            {
                double[] re = null;
                try
                {
                    re = cad.Out_caculation(section.F_Section);
                }
                catch
                {
                    return;
                }
                jf_tree.Nodes.Clear();
                for (int i = 0; i < re.Length; i++)
                {
                    Node n = new Node();
                    n.Text = (i + 1).ToString();
                    n.Cells.Add(new Cell(re[i].ToString()));
                    jf_tree.Nodes.Add(n);
                }
                jf_tree.Refresh();
            }
            double startA, endA, stepA;
            m_prj.GetAngleStep(out startA, out endA, out stepA);
            TunnelSectionPoints pts = cad.OutPut_Result(startA, endA, stepA);
            for (int i = 0; i < pts.Points.Count; i++)
            {
                //****************
                Node n = new Node();
                n.Text = pts.Points[i].Name;
                string t = (startA + stepA * i).ToString();
                n.Cells.Add(new Cell(t));
                double m = Math.Round(pts.Points[i].Err_Distance * 1000, 1);
                n.Cells.Add(new Cell(m.ToString()));
                tree_re.Nodes.Add(n);
            }
            tree_re.Refresh();
            tabControlPanel4.UseWaitCursor = false;

        }

        //保存到数据库
        private void btn_export_excel_Click(object sender, EventArgs e)
        {

            save();
        }
        void save()
        {
            if (MessageBox.Show("确定入库?\r\n" + ComBo.SelectedItem.ToString() + ":" + section.SectNo, "Conform", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }

            //入库坐标系
            string table_name = "qj_" + section_frm.m_prj.id.ToString();
            string sql; DataTable dt;
            //if (section_frm.m_prj.no == 1)
            //{
            //    sql = "Delete from [" + table_name + "]  where [断面号]='" + section.SectNo + "'";
            //    m_db.ExecuteSQL(sql);
            //}
            sql = "select [BH],[BV],[H],[V] from [" + table_name + "] where [断面号]='" + section.SectNo + "'";
            dt = m_db.GetDataSet(sql).Tables[0];
            double H, V, BH, BV;
            V = 0; H = 0;
            string sql_HVBHBV = "";
            if (dt.Rows.Count > 0 && this.no>1)
            {

                //如果坐标系已经存在,那么就平移坐标,并且赋值.
                // exsit = true;
                BH = Convert.ToDouble(dt.Rows[0][0]);
                BV = Convert.ToDouble(dt.Rows[0][1]);
                H = Convert.ToDouble(dt.Rows[0][2]);
                V = Convert.ToDouble(dt.Rows[0][3]);
            }
            else if(no==1)
            {
                BH = 0;
                BV = 0 - section.InstrHeight;
                H = cad.center.Coor_H;
                V = cad.center.Coor_V;
                //如果屁都没有


                sql_HVBHBV = "Insert into [" + table_name + "] values('" + section.SectNo + "',-1,-1,'','"
                    + RL + "'," + H + "," + V
                    + "," + BH + "," + BV + ",'','')";

                ////如果有里程数据,但是没有坐标系的参数
                //else if (dt.Rows[0]["BH"].ToString() == "-1" || dt.Rows[0]["BH"].ToString().Length == 0)
                //{
                //    sql_HVBHBV = "Update [" + table_name + "] Set [BV]=" + BV + ",[H]=" + H + ",[V]=" + V
                //        + " Where [断面号]='" + section.SectNo + "'";
                //}
                m_db.ExecuteSQL(sql_HVBHBV);
            }


            //入库拟合参数
           
            table_name = table + "_" + no.ToString();

            sql = "Delete from [" + table_name + "] where [断面号]='" + section.SectNo + "'";
            m_db.ExecuteSQL(sql);
            sql = "Insert into [" + table_name + "] values('" + section.SectNo + "'";
            double[] angles = new double[4];
            for (int i = 0; i < Section_Pts.Count; i++)
            {
                sql = sql + "," + Section_Pts[i].center.Coor_H + "," + Section_Pts[i].center.Coor_V + "," + Section_Pts[i].Radius;
            }
            for (int i = 1; i < 5; i++)
            {
                sql = sql + "," + cad.angels[i];
            }
            sql += ")";
            m_db.ExecuteSQL(sql);



            //入库变形结果
            table_name = table_name + "_data";

            if (tree_re.Nodes.Count == 0)
            {
                MessageBox.Show("没有变形数据!");
                return;
            }
            sql = "Delete from [" + table_name + "] where [断面号]='" + section.SectNo + "'";
            m_db.ExecuteSQL(sql);

            for (int i = 0; i < tree_re.Nodes.Count; i++)
            {
                Node n = tree_re.Nodes[i];
                double t = Convert.ToDouble(n.Cells[1].Text);
                double m = Convert.ToDouble(n.Cells[2].Text);

                if (i <= 5)
                {
                    sql = "Insert into [" + table_name + "] values('" + section.SectNo + "'," + t + "," + m +
                        ",'" + (cad.F_Points.Points[i].Coor.Coor_H - H).ToString() + "','" + (cad.F_Points.Points[i].Coor.Coor_V - V).ToString() + "')";
                    m_db.ExecuteSQL(sql);
                }
                else
                {
                    sql = "Insert into [" + table_name + "] values('" + section.SectNo + "'," + t + "," + m + ",'','')";
                    m_db.ExecuteSQL(sql);
                }

            }

            if (section_frm.unsolve_tree.SelectedNode != null)
            {
                int index = section_frm.unsolve_tree.SelectedNode.Index;
                if (section_frm.LR == "L")
                {
                    section_frm.unsolve.l_List.RemoveAt(index);
                    if (section_frm.unsolve.l_List.Count == 0 && section_frm.unsolve.r_List.Count == 0)
                    {
                        string path = Application.StartupPath + @"\Logs\" + section_frm.Text + ".txt";
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                    }
                }
                else
                {
                    section_frm.unsolve.r_List.RemoveAt(index - section_frm.unsolve.l_List.Count);
                    if (section_frm.unsolve.l_List.Count == 0 && section_frm.unsolve.r_List.Count == 0)
                    {
                        string path = Application.StartupPath + @"\Logs\" + section_frm.Text + ".txt";
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                    }
                }
                section_frm.Load_unsolved();

            }
            section_frm.Load_tree();
            Label.Text = "入库成功!";
        }
        #endregion



      

        //点击delete,删除某个数据

        private void btn_reload_Click(object sender, EventArgs e)
        {
            if (path.Length > 0)
            {
                read_raw_file(path);
            }
        }

        private void advTree_KeyDown(object sender, KeyEventArgs e)
        {
            Node n=advTree.SelectedNode ;
            if (n== null)
            {
                return;
            }
            if (e.KeyCode != Keys.Delete)
            {
                return;
            }
            int index = n.Index;
            advTree.Nodes.RemoveAt(index);
            section.SectPnts.Points.RemoveAt(index);
            load_tree_nodes();
            advTree.SelectedNode = advTree.Nodes[index];


            if (advTree.Nodes.Count == 0)
            {
                return;
            }

            GraphPane myPane = zed.GraphPane;
            if (myPane.CurveList.Count > 1)
            {
                myPane.CurveList.RemoveAt(1);
            }

            Draw_ZedGraph();
            n = advTree.SelectedNode;
            if (n == null)
            {
                return;
            }
            double H = Convert.ToDouble(n.Cells[3].Text);
            double V = Convert.ToDouble(n.Cells[2].Text);
            PointPairList list = new PointPairList();
            list.Add(H, V);
            LineItem line = myPane.AddCurve("选中点", list, Color.Red);
            line.Symbol.Fill = new Fill(Color.Red);
            zed.Refresh();
        }

        private void advTree_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            if (advTree.Nodes.Count == 0)
            {
                return;
            }
        
            GraphPane myPane = zed.GraphPane;
            if (myPane.CurveList.Count > 1)
            {
                myPane.CurveList.RemoveAt(1);
            }
            Node n = e.Node;
            if (n == null)
            {
                return;
            }
            //int index = n.Index;
            //myPane.CurveList[0][index].ColorValue = 255;
            //myPane.CurveList[0].Points[index].ColorValue = 18;
            double H = Convert.ToDouble(n.Cells[3].Text);
            double V = Convert.ToDouble(n.Cells[2].Text);
            PointPairList list = new PointPairList();
            list.Add(H, V);
            LineItem line = myPane.AddCurve("选中点", list, Color.Red);
            line.Symbol.Fill = new Fill(Color.Red);
            zed.Refresh();
            
        }


 

        //调整数据的值
        private void Change_Point_Value(double Dh, double Dv, int index)
        {
            if (zed.Selected_Point == false || index < 0)
            {
                return;
            }
            //先把图上显示的坐标都改一下
            GraphPane myPane = zed.GraphPane;
            if (myPane.CurveList.Count > 1)
            {
                myPane.CurveList.RemoveAt(1);
            }

            myPane.CurveList[0].Points[index].X += Dh;
            myPane.CurveList[0].Points[index].Y += Dv;
            double H = Convert.ToDouble(myPane.CurveList[0].Points[index].X);
            double V = Convert.ToDouble(myPane.CurveList[0].Points[index].Y);
            PointPairList list = new PointPairList();
            list.Add(H, V);
            LineItem line = myPane.AddCurve("选中点", list, Color.Red);
            line.Symbol.Fill = new Fill(Color.Red);
            zed.Refresh();


            section.SectPnts.Points[index].Coor.Coor_H = H;
            section.SectPnts.Points[index].Coor.Coor_V = V;
 
            

            Node n = advTree.Nodes[index];
            n.Cells[2].Text = V.ToString("0.000");
            n.Cells[3].Text = H.ToString("0.000");

        }


        //允许用户调整坐标的值
        private void zed_KeyPress(object sender, KeyPressEventArgs e)
        {
            double dh = 0;
            double dv = 0;
            if (e.KeyChar == 'a' || e.KeyChar == 'A')
            {
                dh = -0.003;
                Change_Point_Value(dh, dv, zed.Point_Id);
            }
            else if (e.KeyChar == 'd' || e.KeyChar == 'D')
            {
                dh = 0.003;
                Change_Point_Value(dh, dv, zed.Point_Id);
            }
            else if (e.KeyChar == 'w' || e.KeyChar == 'W')
            {
                dv = 0.003;
                Change_Point_Value(dh, dv, zed.Point_Id);
            }
            else if (e.KeyChar == 's' || e.KeyChar == 'S')
            {
                dv = -0.003;
                Change_Point_Value(dh, dv, zed.Point_Id);
            }
  
        }

        private void zed_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }
           
            if (zed_cursor == false)
            {
                if (zed.Selected_Point == true && zed.Point_Id >= 0)
                {
                    int index = zed.Point_Id;
                    GraphPane myPane = zed.GraphPane;
                    if (myPane.CurveList.Count > 1)
                    {
                        myPane.CurveList.RemoveAt(1);
                    }
                    try
                    {

                        double H = Convert.ToDouble(myPane.CurveList[0].Points[index].X);
                        double V = Convert.ToDouble(myPane.CurveList[0].Points[index].Y);
                        PointPairList list = new PointPairList();
                        list.Add(H, V);
                        LineItem line = myPane.AddCurve("选中点", list, Color.Red);
                        line.Symbol.Fill = new Fill(Color.Red);
                        zed.Refresh();
                    }
                    catch
                    {
                        return;
                    }
                }
               
            }
            else
            {
                PointF mousept = new PointF(e.X, e.Y);

                double x = zed.GraphPane.XAxis.Scale.Max-zed.GraphPane.XAxis.Scale.Min;
                double y = zed.GraphPane.YAxis.Scale.Max - zed.GraphPane.YAxis.Scale.Min;
                zed_cursor = false;
                btn_add_pt.Enabled = true;
                double left = e.X - zed.GraphPane.Chart.Rect.Left;
                double bottom = zed.GraphPane.Chart.Rect.Bottom - e.Y;
                x = left / zed.GraphPane.Chart.Rect.Width * x + zed.GraphPane.XAxis.Scale.Min;
                y = bottom / zed.GraphPane.Chart.Rect.Height * y + zed.GraphPane.YAxis.Scale.Min;
                SectionPoint pt = new SectionPoint();
                pt.Coor = new TSPCoor(y, x);
                pt.Hv = pt.AzimFrom(section.ButtomCenter);
                section.SectPnts.Points.Add(pt);
                section.SectPnts.SortByHv();
                load_tree_nodes();
                Draw_ZedGraph();

            }
        }

        private void zed_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Delete)
            {
                return;
            }
            int index = zed.Point_Id;
            if (index < 0 )
            {
                return;
            }
            try
            {
                advTree.Nodes.RemoveAt(index);
                section.SectPnts.Points.RemoveAt(index);
                GraphPane myPane = zed.GraphPane;
                if (myPane.CurveList.Count > 1)
                {
                    myPane.CurveList.RemoveAt(1);
                }
                if (index == advTree.Nodes.Count)
                {
                    index = index - 1;
                }
                Draw_ZedGraph();
                // advTree.SelectedNode = advTree.Nodes[index];


                if (advTree.Nodes.Count == 0)
                {
                    return;
                }




                Node n = advTree.SelectedNode;
                if (n == null)
                {
                    return;
                }
                double H = Convert.ToDouble(n.Cells[3].Text);
                double V = Convert.ToDouble(n.Cells[2].Text);
                PointPairList list = new PointPairList();
                list.Add(H, V);
                LineItem line = myPane.AddCurve("选中点", list, Color.Red);
                line.Symbol.Fill = new Fill(Color.Red);
                zed.Refresh();
            }
            catch
            {
                return;
            }
        }


        #region 在zed里边添加新的点
        private void btn_add_pt_Click(object sender, EventArgs e)
        {
            if (zed.GraphPane.CurveList.Count == 0 )
            {
                return;
            }

            zed_cursor = true;
            btn_add_pt.Enabled = false;
        }
        #endregion



        #region 注册快捷键
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(
         IntPtr hWnd, // handle to window 
         int id, // hot key identifier 
         KeyModifiers fsModifiers, // key-modifier options 
         Keys vk // virtual-key code 
        );

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
         IntPtr hWnd, // handle to window 
         int id // hot key identifier 
        );
        [Flags()]
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Control = 2,
            Shift = 4,
            Windows = 8
        } 

        private void add_in_frm_Load(object sender, EventArgs e)
        {
            RegisterHotKey(Handle, 100, KeyModifiers.Control, Keys.D);
            RegisterHotKey(Handle, 200, KeyModifiers.Control, Keys.B);
            RegisterHotKey(Handle, 300, KeyModifiers.Control, Keys.R);
            RegisterHotKey(Handle, 400, KeyModifiers.Control, Keys.A);
            RegisterHotKey(Handle, 500, KeyModifiers.None, Keys.Escape);
        }
        protected override void WndProc(ref Message m)//监视Windows消息
        {
            const int WM_HOTKEY = 0x0312;//按快捷键
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    ProcessHotKey(m);//调用主处理程序
                    break;
            }
            base.WndProc(ref m);
        }

        private void ProcessHotKey(Message m)
        {
            if (this.ActiveControl.Focused != true)
            {
                return;
            }
            IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
            string sid = id.ToString();
            switch (sid)
            {
                case "100":
                    simulate();
                    break;
                case "200":
                    save();
                    break;
                case "300":
                    {
                        if (path.Length > 0)
                        {
                            read_raw_file(path);
                        }
                        break;
                    }
                case "400":
                    {
                        if (zed.GraphPane.CurveList.Count == 0 || zed_cursor == true)
                        {
                            return;
                        }

                        zed_cursor = true;
                        btn_add_pt.Enabled = false;
                        break;
                    }
                case "500":
                    this.Close();
                    break;

            }

        }
        private void add_in_frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            UnregisterHotKey(Handle, 100);
            UnregisterHotKey(Handle, 200);
            UnregisterHotKey(Handle, 300);
            UnregisterHotKey(Handle, 400);
            UnregisterHotKey(Handle, 500);
        }
        #endregion










    }
}